package com.example.libary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libary.common.Result;
import com.example.libary.entity.User;
import com.example.libary.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户控制器
 */
@Tag(name = "用户管理", description = "用户信息管理相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "根据ID获取用户信息")
    @GetMapping("/{id}")
    public Result<User> getUser(@Parameter(description = "用户ID") @PathVariable Long id) {
        User user = userService.getById(id);
        if (user != null) {
            user.setPassword(null); // 不返回密码
        }
        return Result.success(user);
    }

    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "更新用户基本信息")
    @PutMapping("/{id}")
    public Result<Void> updateUser(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "用户信息") @RequestBody @Valid User user) {
        user.setId(id);
        user.setPassword(null); // 不允许通过此接口修改密码
        boolean success = userService.updateById(user);
        return success ? Result.success() : Result.error("更新失败");
    }

    /**
     * 修改密码
     */
    @Operation(summary = "修改密码", description = "修改用户密码")
    @PutMapping("/{id}/password")
    public Result<Void> updatePassword(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "密码信息") @RequestBody Map<String, String> passwordInfo) {
        String oldPassword = passwordInfo.get("oldPassword");
        String newPassword = passwordInfo.get("newPassword");
        boolean success = userService.updatePassword(id, oldPassword, newPassword);
        return success ? Result.success() : Result.error("修改密码失败");
    }

    /**
     * 分页查询用户列表
     */
    @Operation(summary = "分页查询用户", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/page")
    public Result<Page<User>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "状态") @RequestParam(required = false) String status) {
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.page(page, username, status);
        // 不返回密码
        userPage.getRecords().forEach(user -> user.setPassword(null));
        return Result.success(userPage);
    }

    /**
     * 禁用/启用用户
     */
    @Operation(summary = "禁用/启用用户", description = "管理员权限")
    @PreAuthorize("hasRole('ADMIN')")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        boolean success = userService.updateById(user);
        return success ? Result.success() : Result.error("操作失败");
    }
} 